<?php
require_once("model/FUserBean.class.php");
require_once("model/FIN_BankListBean.class.php");
require_once("admin/includes/util.php");
class FUser {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty,$page='',$cat='',$product='') {
		$this->db = $db;
		$this->smarty = $smarty;
		$this->page = $page;
		$this->cat = $cat;
		$this->product = $product;
	}
	
	public function main() {
		if(isset($_POST['act']) && $_POST['act']=='newuser')
			$this->newCustomer();
		else if(isset($_POST['act']) && $_POST['act']=='login')	
			$this->login();
		else if(isset($_POST['act']) && $_POST['act']=='save_eccurency_user')	
			$this->save_currency_user();	
		else{	
			if($this->cat == 'edit' && isset($_SESSION['cust_id'])){
				$this->smarty->display("user/edit.tpl");	
			}else if($this->cat == 'logout' && isset($_SESSION['cust_id'])){	
				$this->logout();	
			}else if($this->cat == 'ecurrencyAccounts' && isset($_SESSION['cust_id'])){	
				$this->ecurrencyAccounts();	
			}else if($this->cat == 'active'){	
				$this->activeUser();		
			}else if($this->page == 'user' && isset($_SESSION['cust_id'])){
				$countryBean = new FCountryBean($db, $smarty);
				$allcountry = $countryBean->get_all_country($this->db);	
				$this->smarty->assign('ALLCOUNTRY',$allcountry);
				$this->smarty->display("user/edit.tpl");	
			}
		}
	}
	
	public function ecurrencyAccounts() {
		$ecurrencylist = FUserBean::getECurrencyUser($this->db,$_SESSION['cust_id']);	
		$this->smarty->assign('ECURRENCYLIST',$ecurrencylist);
		$this->smarty->display("user/ecurrencyAccounts.tpl");	
	}
	
	public function save_currency_user() {
		$ecurrencylist = FIN_BankListBean::getECurrency($this->db);	
		if($ecurrencylist!=''){
			foreach($ecurrencylist as $item){
				if($_POST['ecurrency_'.$item->id] !=''){
					$exist = FUserBean::checkExistCurrencyAccount($this->db,$_SESSION['cust_id'],$item->id);	
					if($exist !='')
						FUserBean::updateCurrencyUser($this->db,$_SESSION['cust_id'],$item->id,$_POST['ecurrency_'.$item->id]);	
					else
						FUserBean::saveCurrencyUser($this->db,$_SESSION['cust_id'],$item->id,$_POST['ecurrency_'.$item->id]);	
				}
			}
		}
		$type = array('ID' => 0, 'message' => 'Save');
		echo array2json($type);	
	}
	
	public function activeUser() {
		
		if($_GET['code'] == crypt($email, '$ntp$')){
			$cus = FUserBean::getActiveCustomerById($this->db, $_GET['id']);
			if($cus->active != '1'){
				FUserBean::activeAccount($this->db, $_GET['id']);
				$return = "1";
			}else{
				$return = "2";
			}
		}else{
			$return = '0';
			$return = "Invalid link!";
		}
		$this->smarty->assign("RETURN",$return);
		$this->smarty->display("user/active.tpl");	
	}
	
	public function logout() {
		unset($_SESSION['cust_id']);
		unset($_SESSION['cust_login']);
		unset($_SESSION['cust_name']);
		unset($_SESSION['err']);
		
		setcookie("fgold[cust_id]", "");
		setcookie("fgold[cust_login]", "");
		setcookie("fgold[cust_name]", "");
			
		header("Location: ../login");
	}
	
	
	
	
	
}
?>
